Sphere intersection
We wish to find out when a ray hits a sphere. Our spheres will be defined by the implicit equation of a sphere (p−c)⋅(p−c)−r2=0
where p) is the a possible position on the sphere, c) is the center of the sphere, and r is the radius.
Given the parametric equation of a ray r=e+td we can solve for the intersection by setting the equations equal to one another. Put the ray equation in place of the p in the sphere equation results in:
(e+td−c)⋅(e+td−c)−r2=0;
This expands to:
(d⋅d)t2+2d⋅(e−c)t+(e−c)⋅(e−c)−r2=0
This result is solvable using the quadratic formula: x=−b±√b2−4ac2a
where
a=d⋅d)
b=2d⋅(e−c)
c=(e−c)⋅(e−c)−r2=0
We can then solve for t: t=−d⋅(e−c)±√(d⋅(e−c))2−(d⋅d)((e−c)⋅(e−c)−r2)(d⋅d)